package starcoast.bocai.com.starcoast.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import starcoast.bocai.com.starcoast.R;
import starcoast.bocai.com.starcoast.bean.TabEntity;
import starcoast.bocai.com.starcoast.common.BaseActivity;
import starcoast.bocai.com.starcoast.fragments.HomeFragment;
import starcoast.bocai.com.starcoast.fragments.LifeFragment;
import starcoast.bocai.com.starcoast.fragments.MovableFragment;
import starcoast.bocai.com.starcoast.fragments.UserFragment;
import starcoast.bocai.com.starcoast.fragments.WorkFragment;

/**
 * Created by admin on 2016/6/3.
 */
public class MainActivity extends BaseActivity {
    private ArrayList<CustomTabEntity> mTabEntities = new java.util.ArrayList<>();
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    @Bind(R.id.ctl_footer)
    CommonTabLayout footer;
    private int[] mIconUnSelectIds = {
            R.mipmap.ic_home,R.mipmap.ic_life,R.mipmap.ic_move,R.mipmap.ic_work,R.mipmap.ic_person
    };
    private int[] mIconSelectIds = {
        R.mipmap.ic_home_select,R.mipmap.ic_life_select,R.mipmap.ic_move_select,R.mipmap.ic_work_select,R.mipmap.ic_person_select
    };
    private String[] mTitles = {"主页", "生活圈", "活动圈", "工作圈", "我的"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        resetFragments(savedInstanceState);
        ButterKnife.bind(this);
        initEvent();
    }

    private void resetFragments(Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            List<Fragment> fragments = getSupportFragmentManager().getFragments();
            if (fragments != null && fragments.size() > 0) {
                boolean showFlag = false;
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                for (int i = fragments.size() - 1; i >= 0; i--) {
                    Fragment fragment = fragments.get(i);
                    if (fragment != null) {
                        if (!showFlag) {
                            transaction.show(fragments.get(i));
                            showFlag = true;
                        } else {
                            transaction.hide(fragments.get(i));
                        }
                    }
                }
                transaction.commitAllowingStateLoss();
            }
        }
    }

    private void initEvent() {
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnSelectIds[i]));
        }
        mFragments.add(new HomeFragment());
        mFragments.add(new LifeFragment());
        mFragments.add(new MovableFragment());
        mFragments.add(new WorkFragment());
        mFragments.add(new UserFragment());
        footer.setTabData(mTabEntities, this, R.id.container, mFragments);
        footer.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                footer.hideMsg(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.container);
        if (fragment != null && fragment instanceof UserFragment) {
            fragment.onActivityResult(requestCode, resultCode, data);
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    protected void onDestroy() {
        mFragments.clear();
        super.onDestroy();
    }
}
